<?php
/**
 * Created by PhpStorm.
 * User: 马超
 * Date: 2017/12/11
 * Time: 16:11
 */

namespace console\controllers;

use app\models\Order;
use app\models\Partner;
use yii\console\Controller;
use yii\console\Exception;

class FtpController extends Controller
{

    public function actionUpload(){

        /*$test = file_get_contents("tbl_partner.txt");
        var_dump($test);exit;*/
        $ftp_server = \Yii::$app->params['ftp']['host'];
        $conn_id = ftp_connect($ftp_server);

        $partners = Partner::find()->where(['partner_status'=>1])->asArray()->all();
        foreach($partners AS $value){
            $login_result = ftp_login($conn_id, $value['ftp_username'], $value['ftp_password']);
            ftp_pasv($conn_id, true);
            $myfile = fopen(date('Y-m').".txt", "w") or die("Unable to open file!");

            $start_time = date('Y-m-01');
            $end_time = date('Y-m-d', strtotime("$start_time +1 month -1 day"))." 23:59:59";
            $start_time = $start_time." 00:00:00";
            $res = Order::find()
                ->select('o.order_no,g.goods_name,g.goods_id,o.goods_num,g.real_price,o.order_amount,u.mobile,o.order_time,o.order_status,o.shipping_status,o.refund_status')
                ->from('{{%order}} o')
                ->leftJoin('{{%goods}} g','g.goods_id=o.goods_id')
                ->leftJoin('{{%user}} u','u.user_id=o.user_id')
                ->leftJoin('{{%mall}} m','m.mall_id=o.mall_id')
                ->where('o.order_status>0')
                ->andWhere(['m.partner_id'=>$value['partner_id']])
                ->andWhere("pay_time>='$start_time'")
                ->andWhere("pay_time<='$end_time'")
                ->asArray()
                ->all();
            foreach($res AS $v){
                if($v['order_status']==1 && $v['shipping_status']==0 && $v['refund_status']==0)
                    $order_status = "待发货";
                elseif($v['order_status']==1 && $v['shipping_status']==1 && $v['refund_status']==0)
                    $value['status'] = "订单完成";
                elseif($v['order_status']==1 && $v['shipping_status']==1 && $v['refund_status']==1)
                    $value['status'] = "申请退款";
                elseif($v['order_status']==1 && $v['shipping_status']==1 && $v['refund_status']==2)
                    $value['status'] = "退款中";
                elseif($v['order_status']==1 && $v['shipping_status']==1 && $v['refund_status']==3)
                    $value['status'] = "订单完成";
                elseif($v['order_status']==1 && $v['shipping_status']==1 && $v['refund_status']==4)
                    $value['status'] = "退款完成";
                $txt = $v['order_no']."\t".$v['goods_name']."\t".$v['goods_id']."\t".$v['goods_num']."\t".$v['real_price']."\t".$v['order_amount']."\t".$v['mobile']."\t".$v['order_time']."\t".$order_status."\r\n";
                fwrite($myfile, $txt);
            }

            fclose($myfile);
            $myfile = fopen(date('Y-m').".txt", "r");
            if (ftp_fput($conn_id, date('Y-m').".txt", $myfile, FTP_ASCII)) {
                echo "successfully uploaded\n";
            } else {
                echo "There was a problem while uploading\n";
            }
            fclose($myfile);
            ftp_close($conn_id);
            exit;
        }

    }



}